package cn.edu.neusoft.yuejian4.yuejian_4.activity;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Train;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.TrainModel;

/**
 * Created by Lix on 2018/10/9.
 */

public class TrainDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tr_name,tr_intro,tr_time,tr_price,tr_price1,tr_address,tr_tel,flag;
    private ImageView tr_img,back,tr_assint,tr_collection;
    private Button signup;
    private SharedPreferences sp;
    private Context mContext;
    private int tid,uid;




    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traindetails);
        initViews();
        initEvent();
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        tid = getIntent().getIntExtra("trainid",1);
        uid = sp.getInt("uid",4);
        TrainModel trainModel = new TrainModel();
        trainModel.trainDetails(tid,listener);
        trainModel.iscollection(tid,uid,iscollectionlistener);


    }
    //0已收藏 1未收藏
    TListener<Success> iscollectionlistener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if(success.getError().equals("0")){
             tr_collection.setImageResource(R.drawable.ic_ycollection);
             flag.setText("1");
            }else if(success.getError().equals("1")){
                tr_collection.setImageResource(R.drawable.ic_nocollection);
                flag.setText("0");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<Success> collectionListener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(TrainDetailActivity.this,"收藏成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> uncollectionListener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(TrainDetailActivity.this,"取消收藏成功！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Train> listener = new TListener<Train>() {
        @Override
        public void onResponse(Train train) {
            tr_name.setText(train.getTrain_name());
            tr_intro.setText(train.getIntro());
            tr_price.setText(Integer.toString(train.getPrice()));
            tr_price1.setText("￥"+Integer.toString(train.getPrice())+"/人");
            tr_time.setText(train.getTime());
            tr_address.setText(train.getAddress());
            tr_tel.setText(Integer.toString(train.getTel()));
            Picasso.with(TrainDetailActivity.this)
                    .load(Server.PIC_URL_J+train.getPic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(tr_img);

        }

        @Override
        public void onFail(String msg) {

        }
    };





    private void initViews(){
        tr_name = (TextView)findViewById(R.id.tr_name);
        tr_intro = (TextView)findViewById(R.id.tr_intro);
        tr_time = (TextView)findViewById(R.id.tr_time);
        tr_address = (TextView)findViewById(R.id.tr_address);
        tr_price = (TextView)findViewById(R.id.tr_price);
        tr_price1 = (TextView)findViewById(R.id.tr_price1);
        tr_tel = (TextView)findViewById(R.id.tr_tel);
        tr_img = (ImageView)findViewById(R.id.tr_img);
        back = (ImageView) findViewById(R.id.tr_back);
        tr_assint = (ImageView) findViewById(R.id.train_assint);
        tr_collection = (ImageView)findViewById(R.id.train_collection);
        signup = (Button)findViewById(R.id.train_signup);
        flag = (TextView)findViewById(R.id.flag_train_collection);
    }

    private void initEvent(){
        back.setOnClickListener(this);
        signup.setOnClickListener(this);
        tr_assint.setOnClickListener(this);
        tr_collection.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tr_back:
                finish();
                break;
            case R.id.train_signup:
                Intent intent = new Intent(TrainDetailActivity.this,TrainSignUpActivity.class);
                intent.putExtra("trainid",tid);
                startActivity(intent);
                break;
            case R.id.train_assint:
                AlertDialog.Builder builder = new AlertDialog.Builder(TrainDetailActivity.this);
                builder.setTitle("客服-帮助");
                builder.setMessage("你确定拨打"+tr_tel.getText()+"请求客服帮助?");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + Integer.parseInt(tr_tel.getText().toString()));
                        intent.setData(data);
                        startActivity(intent);

                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                    break;
            case R.id.train_collection:
                TrainModel trainModel = new TrainModel();
                if(flag.getText().equals("0")){
                    trainModel.collection(tid,uid,collectionListener);
                    tr_collection.setImageResource(R.drawable.ic_ycollection);
                }else if(flag.getText().equals("1")){
                    trainModel.uncollection(tid,uid,uncollectionListener);
                    tr_collection.setImageResource(R.drawable.ic_nocollection);
                }
                break;
        }

    }


}
